<!DOCTYPE html>
<html>

<head lang="en">
    <title>游记点评</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/strategyComment.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script src="/js/common.js"></script>
    <link rel="stylesheet" href="/css/ModelCss.css"/>
</head>

<style>
    .usefulClick {
        color: #F32D27;
    }
</style>


<script>
    $(function () {
        //获取地址栏上的id值
        var params = getParams();
        var userStr = sessionStorage.getItem("user");
        var userJSON = JSON.parse(userStr);
        if (params.id) {
            $.get("/travels/" + params.id, function (data) {
                $(".container").renderValues(data);

                //获取文章的点赞次数
                $.get("/travels/" + params.id + "/travelConcerns", function (data) {
                    //本游记点赞次数的回显
                    $(".d-flex .likeNum").html(data.likeNums);
                })


                //评论模态框的展示
                $("#travelBtn").click(function () {
                    $("#content").val("");
                    modal.style.display = "block";
                })
                $("#cancel").click(function () {
                    modal.style.display = "none";
                })


                //确定按钮
                $("#sure").click(function () {
                    //获取文本域的内容
                    var content = $("#content").val();
                    //判断文本域中的内容是否为空
                    if (!content) {
                        $(document).dialog({
                            autoClose: 2500,
                            content: "内容不能为空",
                        });
                        return;
                    }
                    //判断是否自己对自己进行回复

                    if (userJSON.id == data.data.author.id) {
                        $(document).dialog({
                            autoClose: 2500,
                            content: "不能对自己进行评论",
                        });
                        return;
                    }

                    var content = $("#content").val();
                    $.ajax({
                        type: "POST",
                        url: "/travels/comment",
                        data: {"travel.id": params.id, "content": content, "commentator.id": userJSON.id},
                        success: function (data) {
                            if (data.success) {
                                //隐藏文本框
                                modal.style.display = "none";
                                location.reload();
                            }
                        }
                    })


                })

                $("#replyBtn").click(function () {
                    $("#content").val("");
                    modal.style.display = "block";
                })





            })


        }







        //--------------------------------收藏按钮-----------------------------------------------
        if (userJSON) {
            //用户如果已经登录并且点评过该文章 为点赞按钮加上样式
            $.get("/users/" + userJSON.id + "/travels/" + params.id + "/concernCollector", function (data) {
                if (data.success) {
                    $("#collectBtn").addClass('usefulClick');
                }
            })
        }

        //收藏按钮
        $("#collectBtn").click(function () {
            //判断用户是否登录
            if (!userJSON) {
                login("亲,请先登录再收藏");
            } else {
                if ($(this).hasClass('usefulClick')) {
                    //游记收藏的取消
                    $.ajax({
                        type: "PUT",
                        url: "/travelConcerns/Collector",
                        data: {"collector.id": userJSON.id, "travel.id": params.id},
                        success: function (data) {
                            if (data.success) {
                                $(document).dialog({
                                    type: 'toast',
                                    infoIcon: '/imgs/fail.png',
                                    infoText: '取消收藏',
                                    autoClose: 2500
                                });
                                $("#collectBtn").removeClass('usefulClick')
                            }
                        }
                    })
                } else {
                    //如果没有加上样式 表示当前用户还没对该游记进行收藏
                    $.ajax({
                        type: "POST",
                        url: "/travelConcerns/Collector",
                        data: {"collector.id": userJSON.id, "travel.id": params.id},
                        success: function (data) {
                            if (data.success) {
                                $(document).dialog({
                                    type: 'toast',
                                    infoIcon: '/imgs/success.png',
                                    infoText: '收藏成功',
                                    autoClose: 2500
                                });
                                $("#collectBtn").addClass('usefulClick');
                            }
                        }
                    })
                }

            }
        })


        //获取评论的内容
        $.get("/travels/" + params.id + "/comment", function (data) {
            console.log(data);
            $("#ofComment").renderValues(data, {
                setHerf: function (ele, val) {
                    var href = $(ele).attr("href") + val;
                    $(ele).attr("href", href);
                },
                setValue: function (ele, val) {

                }
            });
            //文章评论的次数
            $("#commentNum").html(data.total);
        })


    })
</script>


<div id="modal" class="modal">
    <div class="modal-content">
        <header class="modal-header">
            <h4>请填写你的评论</h4>
        </header>
        <textarea id="content"></textarea>
        <footer class="modal-footer">
            <button id="cancel">取消</button>
            <button id="sure">确定</button>
        </footer>
    </div>
</div>


<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col">
            <a href="/travelPage.html">
                <span><i class="fa fa-chevron-left"></i></span>
            </a>
        </div>
        <div class="col">
            <span>点评详情</span>
        </div>
        <div class="col"></div>
    </div>
</div>

<div class="comment">


    <div class="container ">
        <div class="row">
            <div class="col-2 comment-head">
                <a id="authorId" data-href="userProfiles.html" render-attr="data-id=data.author.id">
                    <img render-src="data.author.headImgUrl" class="rounded-circle">
                </a>

            </div>

            <div class="col">
                <span class="comment-date" render-html="data.lastUpdateTime"></span>
                <span class="comment-img" render-html="data.author.nickName"></span>
                <div class="comment-content">
                    <p render-html="content"></p>
                </div>
                <button class="btn" id="replyBtn">回复</button>
                <button class="btn" id="followBtn">关注</button>
            </div>

        </div>
    </div>

    <div class="count d-flex justify-content-between">
        <div class="p-2">评论<span id="commentNum">0</span></div>
        <!--子点赞-->
        <div class="p-2">赞<span class="likeNum">0</span></div>
    </div>

</div>

<!--评论列表的展示-->
<span>所有回复...</span>
<div class="comment" id="ofComment">

    <div class="container" render-loop="list">
        <div class="row">
            <div class="col-2 comment-head">
                <a href="userProfiles.html?id=" render-fun="setHerf" render-key="list.commentator.id">
                    <span render-html="list.commentator.nickName"></span>
                    <img class="rounded-circle" render-src="list.commentator.headImgUrl">
                </a>
            </div>
            <div class="col">
                <span class="comment-date" render-html="list.date"></span>
                <div class="comment-content">
                    <p render-html="list.content"></p>
                </div>
            </div>

            <div class="d-flex icon">
                <input type="hidden" name="CommUser" render-value="list.commentator.id">
                <a data-href="/mine/strategyComment.html">
                    <i class="fa  fa-commenting-o commentBtn1" render-attr="data-id=list.id" style="margin-right: 10px"
                       content="">
                        <span render-html="list.subComms"></span> 回复 </i>
                    <i class="fa fa-thumbs-o-up likeBtn" render-attr="data-id=list.id"> <span render-html="list.likeNums"></span> 赞</i>
                </a>
            </div>

        </div>
    </div>

</div>


<!--<div class="operation">
    <div class="d-flex justify-content-between">
        <div class="p-2" id="releaseComment"><i class="fa fa-commenting-o"></i> 添加你的评论</div>
        <div class="p-2" id="collectBtn"><i class="fa fa-star-o"></i> 收藏</div>
    </div>
</div>-->


</body>

<script>
    //评论的点赞
    $(".container").on("click", ".likeBtn", function () {
        //评论的ID
        var commId = $(this).data("id");
        console.log(commId);
        var userStr = sessionStorage.getItem("user");
        var userJSON = JSON.parse(userStr);
        if (userJSON) {
            //评论点赞请求
            $.ajax({
                type: "POST",
                url: "/ofComments/travel",
                data: {"comment.id": commId, "liker.id": userJSON.id},
                success: function (data) {
                    location.reload();
                }
            });
        }

    })
    //回复
    $(".container").on("click",".commentBtn1",function () {
        var commId = $(this).data("id");
        window.location.href = "/mine/subTravelComment.html?id="+ commId ;
    })
</script>


</html>